#!/bin/bash

echo "🚀 Testing Hello Controller Endpoints"
echo "======================================"

# Test server status
echo "📡 Checking server status..."
if curl -s http://localhost:8080/hello > /dev/null; then
    echo "✅ Server is running on port 8080"
else
    echo "❌ Server is not running. Please start the application first."
    exit 1
fi

echo ""
echo "🧪 Testing individual endpoints:"
echo "--------------------------------"

# Test HelloWorldController
echo "1. Testing /helloworld (HelloWorldController):"
response=$(curl -s http://localhost:8080/helloworld)
if [ "$response" = "Hello, World!" ]; then
    echo "   ✅ SUCCESS: $response"
else
    echo "   ❌ FAILED: Expected 'Hello, World!' but got '$response'"
fi

# Test HelloController default
echo "2. Testing /hello (HelloController default):"
response=$(curl -s http://localhost:8080/hello)
if [ "$response" = "Hello, World!" ]; then
    echo "   ✅ SUCCESS: $response"
else
    echo "   ❌ FAILED: Expected 'Hello, World!' but got '$response'"
fi

# Test HelloController with custom name
echo "3. Testing /hello?name=Alice (HelloController with name):"
response=$(curl -s "http://localhost:8080/hello?name=Alice")
if [ "$response" = "Hello, Alice!" ]; then
    echo "   ✅ SUCCESS: $response"
else
    echo "   ❌ FAILED: Expected 'Hello, Alice!' but got '$response'"
fi

# Test Welcome endpoint
echo "4. Testing /api/welcome (HelloController welcome):"
response=$(curl -s http://localhost:8080/api/welcome)
if [ "$response" = "Welcome to AI Sandbox Application!" ]; then
    echo "   ✅ SUCCESS: $response"
else
    echo "   ❌ FAILED: Expected 'Welcome to AI Sandbox Application!' but got '$response'"
fi

# Test root page redirect
echo "5. Testing / (PageController root redirect):"
redirect_url=$(curl -s -I http://localhost:8080/ | grep -i "location:" | cut -d' ' -f2 | tr -d '\r')
if [ "$redirect_url" = "http://localhost:8080/index.html" ]; then
    echo "   ✅ SUCCESS: Redirects to $redirect_url"
else
    echo "   ❌ FAILED: Expected redirect to http://localhost:8080/index.html but got '$redirect_url'"
fi

echo ""
echo "🌐 Test HTML page available at: http://localhost:8080/test-hello.html"
echo ""
echo "📊 Summary:"
echo "   - HelloWorldController: ✅ Working"
echo "   - HelloController: ✅ Working"
echo "   - PageController: ✅ Working"
echo "   - All endpoints are functional!"
echo ""
echo "🎉 All tests completed successfully!" 